package org.adl.registry.website;

import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.adl.registry.website.lucene.QueryProcessor;

/**
 * Provides methods for preprocessing and relaying to RIM a search query submitted via the ADL Registry website Basic Search form.
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class BasicSearchRequest extends SearchRequestHandler
{
    /**
     * Instantiates a new instance of the <CODE>BasicSearchHandler</CODE> class.
     * @param request The <CODE>ServletRequest</CODE> generated by the Servlet receiving the submission of a Form based ADL Registry request.
     */
    public BasicSearchRequest(HttpServletRequest iRequest, ServletContext iContext)
    throws InvalidRequestException
    {
        super();
        
        super.setServletRequest(iRequest);
        super.setServletContext(iContext);

        String registry = iRequest.getParameter("registry"); // This parameter value should be the parameter containing the target registry.
        super.setRegistry(registry);
        
        QueryProcessor parser = new QueryProcessor();
        String query = iRequest.getParameter("search"); // This parameter value should be the parameter containing the search query to be passed to rim.
        query = parser.PreProcess(query);
        super.setQuery(query);
        
        String rimUrl = PortalUtil.getRimUrl(iContext, registry);
        super.setRimUrl(rimUrl);
        
        String cak = PortalUtil.getClientAccessKey(iContext);
        super.setClientAccessKey(cak);
    }

    /**
     * Initiates and carries out a communication session with RIM Lite.
     * @return Returns the <CODE>String</CODE> response from a request to RIM Lite. Generally these responses are always XML.
     * @throws java.io.IOException Throws <CODE>IOException</CODE> if communication with RIM Lite fails.
     */
    public String InvokeRequest()
    throws IOException, InvalidRequestException
    {
        String rimResponse = "";
        
        rimResponse = super.sendSearchRequest();
        
        return rimResponse;
    }
}
